/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lvyh.lightframe.job.admin.config;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ConsumerConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.ProviderConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.spring.AnnotationBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

@Configuration
public class DubboConfig {

    @Autowired
    private Environment environment;

    @Bean
    public static AnnotationBean annotationBean(@Value("com.lvyh.lightframe.job.admin.service.api") String packageName) {
        AnnotationBean annotationBean = new AnnotationBean();
        annotationBean.setPackage(packageName);
        return annotationBean;
    }

    @Bean
    public ProviderConfig providerConfig(ApplicationConfig applicationConfig, RegistryConfig registryConfig, ProtocolConfig protocolConfig) {
        ProviderConfig providerConfig = new ProviderConfig();
        providerConfig.setTimeout(Integer.parseInt(this.environment.getProperty("dubbo.provider.timeout", "10000")));
        providerConfig.setRetries(Integer.parseInt(this.environment.getProperty("dubbo.provider.retries", "1")));
        providerConfig.setDelay(Integer.parseInt(this.environment.getProperty("dubbo.provider.delay", "-1")));
        providerConfig.setApplication(applicationConfig);
        providerConfig.setRegistry(registryConfig);
        providerConfig.setProtocol(protocolConfig);
        return providerConfig;
    }

    @Bean
    public ProtocolConfig protocolConfig() {
        ProtocolConfig protocolConfig = new ProtocolConfig();
        protocolConfig.setName(this.environment.getProperty("dubbo.protocol.name", "dubbo"));
        protocolConfig.setPort(Integer.parseInt(this.environment.getProperty("dubbo.protocol.port", "20889")));
        protocolConfig.setThreads(200);
        return protocolConfig;
    }


    @Bean
    public ApplicationConfig applicationConfig() {
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName(this.environment.getProperty("dubbo.application.name"));
        applicationConfig.setOwner(this.environment.getProperty("dubbo.application.owner"));
        applicationConfig.setRegistry(registryConfig());
        applicationConfig.setMonitor(monitorConfig());
        return applicationConfig;
    }

    @Bean
    public com.alibaba.dubbo.config.MonitorConfig monitorConfig() {
        return new com.alibaba.dubbo.config.MonitorConfig();
    }

    @Bean
    public RegistryConfig registryConfig() {
        RegistryConfig registryConfig = new RegistryConfig();

        registryConfig.setAddress(this.environment.getProperty("dubbo.registry.address"));
        registryConfig.setFile(this.environment.getProperty("dubbo.cache.file"));
        registryConfig.setTimeout(20000);
        return registryConfig;
    }


    @Bean
    public ConsumerConfig consumerConfig() {
        ConsumerConfig consumerConfig = new ConsumerConfig();
        consumerConfig.setCheck(false);
        consumerConfig.setTimeout(10000);
        return consumerConfig;
    }
}
